(function() {
    var dialog = require("wap_common:widget/ui/dialog/dialog.js").dialog(),
        ShareTip = require("wap_common:widget/ui/sharetip/sharetip.js").ShareTip,
        share,shareTip;

    P.share.data({
        onSendFriends:function(res){
            if(res.err_msg == "share_timeline:ok"){
                $.get("/wap/sharestatistic",function(ret,status){});
            }
        }
    });

    //分享的提示
    shareTip = new ShareTip({
        content:'<div>每周分享到朋友圈能提升额度，共有3次提升额度的机会</div>'
    });

	var rechargePage = {
		init:function() {
			this.util = require("wap_common:widget/ui/libs/util.js").util;
			this.id_card = null;
			this.gate_id = null;
			this.eventCenter();
			this.getPlatFund();
//			this.getValidateInfo();
		},
		eventCenter: function() {
			var me = this;
			var ImgClickEvent = function(event) {
				if(event.preventDefault){
				    event.preventDefault();
				}else{
				    event.returnValue = false;
				}
				if($(this).attr('id') == 'more_bank') {
					var more_bank = '';
					more_bank += '<a id="g16" href="#"><img src="/static/home/lib/img/bank/Bank08.jpg"></a>';
					more_bank += '<a id="g09" href="#"><img src="/static/home/lib/img/bank/Bank09.jpg"></a>';
					more_bank += '<a id="g13" href="#"><img src="/static/home/lib/img/bank/Bank10.jpg"></a>';
					more_bank += '<a id="g19" href="#"><img src="/static/home/lib/img/bank/Bank11.jpg"></a>';
					more_bank += '<a id="g12" href="#"><img src="/static/home/lib/img/bank/Bank12.jpg"></a>';
					more_bank += '<a id="g33" href="#"><img src="/static/home/lib/img/bank/Bank13.jpg"></a>';
					more_bank += '<a id="g36" href="#"><img src="/static/home/lib/img/bank/Bank14.jpg"></a>';
					more_bank += '<a id="g55" href="#"><img src="/static/home/lib/img/bank/Bank15.jpg"></a>';
					more_bank += '<a id="g50" href="#"><img src="/static/home/lib/img/bank/Bank16.jpg"></a>';
					more_bank += '<a id="g15" href="#"><img src="/static/home/lib/img/bank/Bank17.jpg"></a>';
					more_bank += '<a id="g40" href="#"><img src="/static/home/lib/img/bank/Bank18.jpg"></a>';
					more_bank += '<a id="g49" href="#"><img src="/static/home/lib/img/bank/Bank19.jpg"></a>';
					more_bank += '<a id="g51" href="#"><img src="/static/home/lib/img/bank/Bank20.jpg"></a>';
					more_bank += '<a id="g53" href="#"><img src="/static/home/lib/img/bank/Bank21.jpg"></a>';
					more_bank += '<a id="g54" href="#"><img src="/static/home/lib/img/bank/Bank22.jpg"></a>';
					$(this).replaceWith(more_bank);
					$('.bank_list a').unbind('click');
					$('.bank_list a').bind('click',ImgClickEvent);
					return;
				}
				//设定gateID
				me.gate_id = $(this).attr('id').substr(1);
				$('.bank_list a').css('borderColor','#CCC');
				$('.bank_list a').css('box-shadow','none');
				$(this).css('borderColor','#F93');
				$(this).css('box-shadow','0px 0px 10px #F93');
			};
			$('.bank_list a').bind('click',ImgClickEvent);
			$('#recharge_button').bind('click',function(event) {
				var buy_amount = $('#buy_amount').val();
				if(!buy_amount) {
                    dialog.show("请输入购买金额");
					return;
				}
                if(buy_amount> P.data("limit_available")){
                    if(P.data('limit_buy')<20000){
                        dialog.show('' +
                            '<div class="limit-available-tip">' +
                            '<div>已超过您剩余可购买额度，您当前剩余可购额度为￥'+ P.data('limit_available')+'</div>' +
                            '<div>分享到朋友圈，可以提升购买份额，<a class="now-share">现在去分享</a></div>'+
                            '</div>'+
                            '');
                        $(".now-share").click(function(){
                            shareTip.showShare();
                        })
                    }else{
                        dialog.show('已超出您可购买的额度');
                    }
                    return;
                }
				if(!me.gate_id) {
                    dialog.show('请选择支付的银行。');
					return;
				}
				if(!$('#read_cb').is(':checked')) {
                    dialog.show('请确认阅读自动交易与债券转让协议。');
					return;
				}
				window.open('/pay/createpayserial?amount=' + buy_amount + '&gate_id=' + me.gate_id);
                dialog.show('请在新的窗口中登录网上银行进行付款。在付款完成后可以在本页面上方查看我的交易记录查看充值结果，如果有问题可以稍后进行查看；如果长时间未收到本次充值的金额，请通过页面下方联系方式联系我们。');
			})
		},
		getPlatFund: function() {
			var me = this;
			$.getJSON('/plat/plat_fund_summary' ,function (data) {
				if(data && data.error_no == 0 && data.data) {
					var usable_credit = parseInt(data.data.plat_total_buy) - parseInt(data.data.plat_credit_buy);
					$('.usable-amount').text('￥' + me.util.toCredit(usable_credit));
					$('.total-amount').text('￥' + me.util.toCredit(data.data.plat_total_buy));
					var percent = me.util.toPercent(data.data.weight_profit_years_percent);
					$('.profit_percent').html(percent+'<span>%</span>');
					// me.renderPie(data.data);
				} else {
					if(data.error_no != 0) {
						alert(data.error_message);
					}
				}
			})
		},
		getValidateInfo : function() {
			var me = this;
			$.getJSON('/account/getuserstatus', function(data) {
				if(data && data.error_no == 0) {
					me.limit_buy = parseInt(data.data.limit_buy);
					$('.recharge_limit').text("￥" . me.util.toCredit(data.data.limit_buy));
				}
			});
		}
	};
	rechargePage.init();
})();
